package com.thor.palmmall.util.pay;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.thoughtworks.xstream.XStream;

public class XmlUtil {

  /**
   * XML转对象
   * 
   * @param xmlStr
   * @param t
   * @return
   */
  @SuppressWarnings("unchecked")
  public static <T> T xmlToBean(String xmlStr, Class<T> t) {
    XStream xstream = XStreamInitializer.getInstance();
    xstream.processAnnotations(t);
    T result = (T) xstream.fromXML(xmlStr);
    return result;
  }

  /**
   * XML转对象
   * 
   * @param t
   * @return
   */
  @SuppressWarnings("unchecked")
  public static <T> T xmlToBean(InputStream input, Class<T> t) {
    try {
      JAXBContext context = JAXBContext.newInstance(t);
      Unmarshaller unmarshaller = context.createUnmarshaller();
      T ts = (T) unmarshaller.unmarshal(new InputStreamReader(input, "UTF-8"));
      return ts;
    } catch (JAXBException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 对象转XML
   * 
   * @param out
   * @param to
   */
  public static String beanToXml(ByteArrayOutputStream out, Object to) {
    try {
      JAXBContext context = JAXBContext.newInstance(to.getClass());
      Marshaller marshaller = context.createMarshaller();
      marshaller.marshal(to, out);
      return new String(out.toByteArray(), "UTF-8");
    } catch (JAXBException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return null;
  }

}
